<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="执行时间">
        <el-date-picker
          v-model="daterangeActime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="执行结果" prop="action">
        <el-select v-model="queryParams.action" placeholder="请选择执行结果" clearable size="small">
          <el-option
            v-for="dict in dict.type.action_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="sysProcessList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="index" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="pid" v-if="true"/> -->
      <el-table-column label="任务" align="center" prop="title" width="150" />
      <!-- <el-table-column label="模型id" align="center" prop="mid" /> -->
      <el-table-column label="进度" align="center" prop="wid" width="260">
          <template slot-scope="scope">
            <span>
            {{scope.row.workflow}}&gt;
            {{scope.row.modelName}}&gt;
            {{scope.row.nodeName}}
            </span>
          </template>
      </el-table-column>
      <!-- <el-table-column label="节点id" align="center" prop="nid" /> -->
      <!-- <el-table-column label="顺序" align="center" prop="seq" /> -->
      <!-- <el-table-column label="名称" align="center" prop="name" /> -->
      <!-- <el-table-column label="描述" align="center" prop="memo" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.node_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="执行人" align="center" prop="operator" />
      <el-table-column label="执行时间" align="center" prop="actime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行结果" align="center" prop="action">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.action_type" :value="scope.row.action"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listSysProcess, getSysProcess, delSysProcess, addSysProcess, updateSysProcess } from "@/api/system/sysProcess";

export default {
  name: "SysProcess",
  dicts: ['node_con', 'action_type', 'node_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务流程表格数据
      sysProcessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 父节点时间范围
      daterangeActime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bussid: undefined,
        mid: undefined,
        wid: undefined,
        nid: undefined,
        name: undefined,
        status: undefined,
        operator: undefined,
        actime: undefined,
        action: undefined,
        cond: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询业务流程列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeActime && '' != this.daterangeActime) {
        this.queryParams.params["beginActime"] = this.daterangeActime[0];
        this.queryParams.params["endActime"] = this.daterangeActime[1];
      }
      listSysProcess(this.queryParams).then(response => {
        this.sysProcessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pid: undefined,
        bussid: undefined,
        mid: undefined,
        wid: undefined,
        nid: undefined,
        seq: undefined,
        name: undefined,
        memo: undefined,
        status: "0",
        operator: undefined,
        actime: undefined,
        action: undefined,
        remark: undefined,
        cond: "0",
        pnid: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeActime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业务流程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const pid = row.pid || this.ids
      getSysProcess(pid).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改业务流程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.pid != null) {
            updateSysProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSysProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pids = row.pid || this.ids;
      this.$modal.confirm('是否确认删除业务流程编号为"' + pids + '"的数据项？').then(() => {
        this.loading = true;
        return delSysProcess(pids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sysProcess/export', {
        ...this.queryParams
      }, `sysProcess_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
